<?php
declare(strict_types=1);

namespace App\AdminApi\Module\System\Service;

use App\AdminApi\Module\System\Traits\ConfigureTraits;
use Zms\AdminElementPlus\Component\Component;
use Zms\AdminElementPlus\FormCreate\FormItem;
use Zms\Unit\Arr;
use Zms\Validate\Type;

class SystemService
{
    use ConfigureTraits;

    #-----------------------------------------------------系统设置------------------------------------------------------#

    /**
     * 获取系统配置
     * @param $configure
     * @return array
     */
    public function getSystemFormRule($configure): array
    {
        return [
            FormItem::Input('company', '公司名称', $configure['company'] ?? '')->placeholder('公司名称')->maxlength(10)->required('请设置公司名称'),
            FormItem::Input('host', '站点域名', $configure['host'] ?? '')->hint('项目站点域名,不设置则使用当前域名'),
            FormItem::Input('domain', '资源域名', $configure['domain'] ?? '')->hint('项目静态资源资源,上传文件域名，不设置则使用当前域名'),
            FormItem::Radio('mourn', '哀悼模式', $configure['mourn'] ?? '0')->options([['value' => '0', 'label' => '关闭'], ['value' => '1', 'label' => '开启']])->info('开启后系统界面只显示黑白色彩'),
        ];
    }


    /**
     * 会员配置验证
     * @return array
     */
    public function getSystemValidateRule(): array
    {
        return [
            'rule' => [
                ['company', Type::REQUIRED],
                ['company', Type::STRING, 'min' => 2, 'max' => 10],

                [['host', 'domain'], Type::DOMAIN, 'port' => true],

                ['mourn', Type::REQUIRED_CHOOSE],
                ['mourn', Type::IN, 'enums' => [1, 0]],
            ],
            'label' => [
                'company' => '公司名称',
                'host' => '站点域名',
                'domain' => '资源域名',
                'mourn' => '哀悼模式'
            ]
        ];
    }


    #-----------------------------------------------------地图设置------------------------------------------------------#

    public function getMapFormRule($configure): array
    {
        return [
            FormItem::Input('web_key', '浏览器端', $configure['web_key'] ?? '')->hint('腾讯地图KEY,用户web浏览器前端页面,需要在腾讯地图控制台创建应用且配置域名白名单'),
            FormItem::Input('ser_key', '小程序/服务端', $configure['ser_key'] ?? '')->hint('腾讯地图KEY,服务器端接口请求key,需要在腾讯地图控制台创建应用且配置IP白名单'),
            Component::Alert('【腾讯地图】[https://lbs.qq.com/dev/console/application/mine]中创建应用')->style(['margin' => '0 0 20px 150px'])
        ];
    }

    /**
     * 会员配置验证
     * @return array
     */
    public function getMapValidateRule(): array
    {
        return [
            'rule' => [
                [['web_key', 'ser_key'], Type::STRING]
            ],
            'label' => [
                'web_key' => '浏览器端KEY',
                'ser_key' => '小程序/服务端KEY'
            ]
        ];
    }

    #-----------------------------------------------------违禁词汇------------------------------------------------------#
    public function getViolateFormRule($configure): array
    {
        return [
            FormItem::Switchs('open', '违禁词检测', (string)($configure['open'] ?? '1'))->required()->activeText('开启检测')->inactiveText('关闭检测'),
            FormItem::Radio('model', '检测模式', $configure['model'] ?? 'warn')
                ->options([['value' => 'warn', 'label' => '警告'], ['value' => 'replace', 'label' => '替换']])
                ->control([
                    [
                        'value' => 'warn',
                        'rule' => [
                            Component::Alert('警告模式:用户提交包含违禁词的内容系统会发出警告', 'success')->style(['margin' => '0 0 20px 150px']),
                        ]
                    ],
                    [
                        'value' => 'replace',
                        'rule' => [
                            Component::Alert('替换模式,用户提交包含违禁词的内容系统会替换为*号', 'warning')->style(['margin' => '0 0 20px 150px']),
                        ]
                    ],
                ])
                ->hint('警告模式:提交时发出警告,替换模式:提交时替换掉违规词汇')
                ->required('请选怎验证模式'),
            FormItem::Labels('words', '词汇管理', $configure['words'] ?? [])
                ->maxlength(20)
                ->limit(2000)
                ->required('请设置违禁词汇')
                ->hint('重复词汇会清除')
        ];
    }

    /**
     * 会员配置验证
     * @return array
     */
    public function getViolateValidateRule(): array
    {
        return [
            'rule' => [
                [['open', 'model'], Type::REQUIRED],
                [['open'], Type::IN, 'enums' => [0, 1]],
                [['model'], Type::IN, 'enums' => ['warn', 'replace']],

                ['words', Type::LIST, 'type' => 'string'],
                ['words', function ($value) {
                    return Arr::unique($value);
                }],
            ],
            'label' => [
                'open' => '检测开关',
                'model' => '检测模式',
                'words' => '违禁词'
            ]
        ];
    }


    #-----------------------------------------------------Ai配置------------------------------------------------------#
    public function getAiFormRule($configure): array
    {
        return [
            FormItem::Switchs('open', '开启AI', (string)($configure['open'] ?? '1'))->required()->activeText('开启')->inactiveText('禁用'),
            Component::Divider('DeepSeek', 'center')->style(['margin' => '0 0 20px 0']),
            FormItem::Switchs('deepseek_open', '是否启用', (string)($configure['open'] ?? '1'))->required()->activeText('开启')->inactiveText('禁用')->control([
                [
                    'value' => '0',
                    'rule' => [
                        Component::Alert('DeepSeek已禁用', 'error')->style(['margin' => '0 0 20px 150px'])
                    ]
                ],
                [
                    'value' => '1',
                    'rule' => [
                        FormItem::Input('deepseek_key', 'KEY', $configure['deepseek_key'] ?? '')->required()->hint('KEY申请地址:https://platform.deepseek.com/api_keys')
                    ]
                ]
            ]),
        ];
    }
}